import http from "@/api/http";
export default {
	namespaced: true,
	state: {
		baseUrl: http.baseUrl,
		userInfo: uni.getStorageSync('userInfo') || {}, //用户的基本信息； 
		mobileInfo: {},
		appid: '',
		setting: uni.getStorageSync('setting') || {}
	},
	getters: {},
	mutations: {
		setUserInfo(state, res) {
			console.log(" setUserInfo res: ", res);
			if (res.nickname == 'null' || res.nickname == null) {
				res.nickname = ''
			}
			const data = {
				...state.userInfo,
				...res
			}
			state.userInfo = res;
			uni.setStorageSync("userInfo", res);
		},
		setSettting(state, res) {
			console.log(" setUserInfo res: ", res);
			const data = {
				...state.setting,
				...res
			}
			state.setting = res;
			uni.setStorageSync("setting", res);
		},
		setToken(state, res) {
			state.token = res;
			uni.setStorageSync("token", res);
		},
		setMobileInfo(state, res) {
			state.mobileInfo = res;
		},
		logOut(state) {
			state.userInfo = {}
			state.token = ''
			uni.removeStorageSync('token')
			uni.removeStorageSync('userInfo')
		}
	},
	actions: {
		async getSetting({
			state,
			commit,
			dispatch,
			rootState
		}, data) {
			return new Promise((re, rj) => {
				http.get('user/get_settings').then(res => {
					if (res.code == 200) {
						commit('setSettting', res.data);
						re(res.data)
					}
				})
			})
		},
		async getUserInfo({
			state,
			commit,
			dispatch,
			rootState
		}, data) {
			return new Promise((re, rj) => {
				http.get('user/get_user_info').then(res => {
					if (res.code == 200) {

						let data = {
							...res.data
						}
						data.avter = data.headimgurl
						data.nickname = data.nickname
						data.user_phone = data.user_phone
						commit('setUserInfo', data);
						re(data)
					}
				})
			})
		}
	},
};